<script type="text/javascript">
$(document).ready(function(){


	$("#FrmPasswordSetup").submit(function(e){
		//e.preventDefault();
		//alert("test");
		var recipient_pass_length = $("#recipient-password").val().length;
		var recipient_pass_val = $("#recipient-password").val();
		var executor_pass_length = $("#executor-password").val().length;
		var executor_pass_val = $("#executor-password").val();

		var ctr,error=0,form_inputs,completeFields=true;
		form_inputs = $("#FrmPasswordSetup").find('.pass-setup');
		form_inputs.each(function(index) {
			if (this.value.length == 0) {
				completeFields = false;
				$(this).addClass("error");
			}
		});

		if(completeFields==true){

			if($("#recipient-password").val()!=''){
				var string_count_res = stringCounter(recipient_pass_length,recipient_pass_val);
				if(string_count_res == 0){
					error =1;
					$("#recipient-password").addClass("error");
				}else{
					//alert("valid");
					$("#recipient-password").removeClass("error");
				}

			}
			 if($("#executor-password").val()!=''){
				var string_count_exe = stringCounter(executor_pass_length,executor_pass_val);
				if(string_count_exe == 0){
					error =1;
					$("#executor-password").addClass("error");
				}else{
					//alert("valid");
					$("#executor-password").removeClass("error");
				}

			}
		}else{
			error = 1;
		}

		if(error!=1){
			var dataString = $("#FrmPasswordSetup").serialize();
			$.ajax({
				url:'<?php echo base_url()?>password_setup/update',
				type:'POST',
				data:dataString,
				success:function(response){
		//alert(response);
					if(parseInt(response) == 1){
						alert("Password Updated Successfully")
						window.location='<?php echo base_url()?>profile';
					}
				}
			});
		}


		return false;
	});


	//for cancel
	$("#ps-cancel").click(function(){

		form_inputs = $("#FrmPasswordSetup").find('.pass-setup');
		form_inputs.each(function(index) {
			$(this).val("");
		});
		return false;
	});

	//for generate recipient password
	$("#generate-recipient-password").click(function(){
		$.ajax({
			url:'<?php echo base_url()?>password_setup/generatePassword',
			success:function(response){
				$("#recipient-password").val(response);
			}
		});

		return false;
	});

	//for generate executor password
	$("#generate-executor-password").click(function(){
		$.ajax({
			url:'<?php echo base_url()?>password_setup/generatePassword',
			success:function(response){
				$("#executor-password").val(response);
			}
		});
		return false;
	});


});
function stringCounter(length,val){
	var var_string_int = 0,var_string=0,accepted=0;
	for(ctr=0;ctr<length;ctr++){
			if(isNaN(val.charAt(ctr))){
				var_string++;
			}else{
				var_string_int++;
			}

		}
		if(var_string >= 5 && var_string_int >= 5 ){
			accepted=1;// true
		}else{
			accepted = 0; //false
		}
		return accepted;
}//end fct;

</script>
<div id = "message-listing">
	<div id = "m-listing-top" style="width:100%;text-align:center;">
		<h2>Password Setup</h2>
	</div>

	<form name="FrmPasswordSetup" id="FrmPasswordSetup" method="POST">
		<div id="password-setup" style="padding-top:60px;">
			<div style="border: 1px solid #C8C8C8;margin-bottom: 10px;padding: 10px;">
				<div style="padding:0px 10px 0px 10px;">
					It is necessary to setup passwords for your executors and recipients so that all your content will be secured from
					any tampering.
				</div>
				<div  style="padding:0px 10px 0px 10px;">
					Passwords should be at least 5 letters and 5 numbers. Click generate button to automatically create passwords.
				</div>
			</div>
			<div style="padding-top:10px;margin: 0 25px;">
				<div>
					<div style="float:left">
					<label style="padding-right:5px;">Recipient's Password :</label>
					<input class="pass-setup" type="text" value="" name="recipient-password" id="recipient-password"/>
					</div>
					<div id="generate-recipient-password">
						Generate Password
					</div>
				</div>
				<div style="margin-top:25px;">
					<div style="float:left">
						<label style="padding-right:10px;">Executor's Password :</label>
						<input class="pass-setup" type="text" value="" name="executor-password" id="executor-password"/>
					</div>
					<div id="generate-executor-password">
						Generate Password
					</div>
				</div>
			</div>
		</div>
		<div class="clear"></div>
		<div class="pass-setup-note">
			<span>Note: Once you already submitted the passwords. You cannot able to change it.</span>
		</div>
		<p style="margin-top: 20px;text-align: center;">
			<input id="ps-submit"  type="submit" value=""/>
			<input id="ps-cancel" type="button" value=""/>
		</p>
	</form>

</div>